package cn.hh.product.service.impl;


import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.basic.util.PageList;
import cn.hh.product.domain.Product;
import cn.hh.product.domain.ProductDetail;
import cn.hh.product.mapper.ProductDetailMapper;
import cn.hh.product.mapper.ProductMapper;
import cn.hh.product.query.ProductQuery;
import cn.hh.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends BasicServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    /*新增或修改*/
    @Override
    public void addOrUpdate(Product product) {
        // 1.增改product
        if (product.getId()==null){
            productMapper.add(product);
            System.out.println("保存后的数据"+product.getId());
        }else {
            productMapper.update(product);
        }
        // 2.判断是否有productdetail，有才执行增改
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            if (productDetail.getId()==null){
                productDetail.setProduct_id(product.getId());
                System.out.println("即将保存的数据"+productDetail);
                productDetailMapper.add(productDetail);
            }else {
                productDetailMapper.update(productDetail);
            }
        }
    }
    /*通过id查详情*/
    @Override
    public ProductDetail loadDetailById(Long id) {
        return productDetailMapper.loadDetailById(id);
    }
    /*批量上架*/
    @Override
    public void onsaleByIds(Long[] ids) {
        productMapper.onsaleByIds(ids);
    }
    /*批量下架*/
    @Override
    public void offsaleByIds(Long[] ids) {
        productMapper.offsaleByIds(ids);
    }
    /*查询上架商品*/
    @Override
    public PageList<Product> listqueryPage(ProductQuery query) {
        Integer total=productMapper.queryCount(query);
        List<Product> bags = productMapper.queryData(query);
        PageList<Product> pageList = new PageList<>(total, bags);
        return pageList;
    }

    /*删除*/
    @Override
    public void deleteById(Long id) {
        productDetailMapper.deleteByProductId(id);
        productMapper.deleteById(id);
    }
}
